/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.List;
import model.HoldFag;
import model.Personale;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

public class PersonaleHandler {

    private Session session;
    
    public PersonaleHandler(Session session){
        this.session = session;
    }
    
    
    public Personale[] hentPersonale(int type) throws HibernateException{ // Samme metode som i LoginControl

        
        String hql = HqlPtype(type);
        
            session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            Personale[] personale = new Personale[resultList.size()];
            for (int i = 0; i < resultList.size(); i++) {
                personale[i] = (Personale) resultList.get(i);
            }
            session.getTransaction().commit();

       
        return personale;
    }

    private String HqlPtype(int type) {
        String hql = "";
        switch (type) {
            case 1:
                hql = "from Personale where pType = 1"; // Lærer
                break;
            case 2:
                hql = "from Personale where pType = 2"; // Pædagog
                break;
            case 3:
                hql = "from Personale where pType = 3"; // Sekretær
                break;
            case 4:
                hql = "from Personale where pType = 4"; // Admin
                break;
        }
        return hql;
    }
    
    public HoldFag[] hentPersonensFag(Personale person){

        Object[] obj = person.getHoldFags().toArray();
        HoldFag[] fagene = new HoldFag[obj.length];
        
        for (int i = 0; i < obj.length; i++) {
            fagene[i] = (HoldFag) obj[i];
        }
        
        return fagene;
    }
    
    public void nyPersonale(Personale person) throws HibernateException{
        
        session.beginTransaction();
        String hql = "from model.Personale";
        session.createQuery(hql);
        session.flush();
        session.save(person);
        session.getTransaction().commit();
        
    }
    
    public void opdaterPersonale(Personale person) throws HibernateException{
        
        session.beginTransaction();
        String hql = "from model.Personale";
        session.createQuery(hql);
        session.flush();
        session.update(person);
        session.getTransaction().commit();
        
    }
    
    public void sletPersonale(Personale person) throws HibernateException{
        session.beginTransaction();
        String hql = "from model.Personale";
        session.createQuery(hql);
        session.flush();
        session.delete(person);
        session.getTransaction().commit();
        
        
    }
    
    public String ptypeTilTekst(char ptype){
        int type = Integer.parseInt(ptype+"");
        
        String str = "";
        
         switch (type) {
            case 1:
                str = "Lærer";
                break;
            case 2:
                str = "Pædagog";
                break;
            case 3:
                str = "Sekretær";
                break;
            case 4:
                str = "Admin";
                break;
        }
        return str;
    }
    
}
